//
//  JackStringExtension.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/16.
//

import Foundation
extension String {
    func substring(from index: Int, length: Int) -> String? {
        guard index >= 0, index < count, length > 0 else { return nil }
        
        let startIndex = self.index(self.startIndex, offsetBy: index)
        let endIndex = self.index(startIndex, offsetBy: length, limitedBy: self.endIndex) ?? self.endIndex
        
        return String(self[startIndex..<endIndex])
    }
    
    func substring(to index: Int) -> String? {
        guard index >= 0, index < count else { return nil }
        let endIndex = self.index(self.startIndex, offsetBy: index + 1)
        return String(self[..<endIndex])
    }
    
    func substring(from index: Int) -> String? {
        guard index >= 0, index < count else { return nil }
        let startIndex = self.index(self.startIndex, offsetBy: index)
        return String(self[startIndex...])
    }
    func isToday(format: String = "yyyy.MM.dd") -> Bool {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        if let inputDate = dateFormatter.date(from: self) {
            return inputDate.isToday()
        }
        return false
    }
    func getPreviousOrNextMonth(monthsToAdd: Int) -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy.MM"

            guard let currentDate = dateFormatter.date(from: self) else {
                return nil
            }

            let calendar = Calendar.current
            if let newDate = calendar.date(byAdding: .month, value: monthsToAdd, to: currentDate) {
                return dateFormatter.string(from: newDate)
            } else {
                return nil
            }
        }
    func getPreviousOrNextDay(daysToAdd: Int) -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy.MM.dd"

            guard let currentDate = dateFormatter.date(from: self) else {
                return nil
            }

            let calendar = Calendar.current
            if let newDate = calendar.date(byAdding: .day, value: daysToAdd, to: currentDate) {
                return dateFormatter.string(from: newDate)
            } else {
                return nil
            }
        }
}
extension NSMutableAttributedString {
    
}
extension NSAttributedString {
    static func coloredString(originalText: String, coloredSubText: String, colorForColoredText: UIColor) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: originalText)
        
        // 查找coloredSubText在原始文本中的范围
        if let range = originalText.range(of: coloredSubText) {
            let nsRange = NSRange(range, in: originalText)
            
            // 设置coloredSubText的颜色
            attributedString.addAttribute(.foregroundColor, value: colorForColoredText, range: nsRange)
        }
        
        return attributedString
    }
    
    static func modifySubstring(_ string: String, targetSubstring: String, color: UIColor, font: UIFont) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: string)
        let range = (string as NSString).range(of: targetSubstring)
        
        if range.location != NSNotFound {
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
            attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: range)
        }
        
        return attributedString
    }
    
}
